from building import *

CWD = GetCurrentDir()
objs = Glob('src/*.c')

objsVXL = Glob('src/vxlapi/*.cpp')

Import('BUILD_DIR')


@register_library
class LibraryCanLib(Library):
    def config_peakcan(self):
        peakcan = Package(
            'https://www.peak-system.com/fileadmin/media/files/pcan-basic.zip')
        self.CPPPATH += ['%s/PCAN-Basic API/Include' % (peakcan)]
        if IsBuildForWin32():
            v0, v1 = 'Win32', 'mingw32'
        else:
            v0, v1 = 'x64', 'mingw64'
        shutil.copy('%s/PCAN-Basic API/%s/VC_LIB/PCANBasic.lib' %
                    (peakcan, v0), '%s/libPCANBasic.a' % (BUILD_DIR))
        dll = '%s/%s/bin/PCANBasic.dll' % (GetMsys2Root(), v1)
        if not os.path.isfile(dll):
            shutil.copy('%s/PCAN-Basic API/%s/PCANBasic.dll' %
                        (peakcan, v0), dll)
        self.LIBS += ['PCANBasic']
        self.LIBPATH += [BUILD_DIR]

    def config_vxl(self):
        self.CPPPATH += ['%s/src/vxlapi' % (CWD)]
        self.CPPDEFINES += ['DYNAMIC_XLDRIVER_DLL']
        self.source += objsVXL

    def config_zlg(self):
        pkg = Package(
            'https://www.zlg.cn/data/upload/software/Can/CAN_lib.rar')
        if IsBuildForWin32():
            ControlCAN = Glob('%s/ControlCAN*/ControlCANx86' % (pkg))[0].rstr()
        else:
            ControlCAN = Glob('%s/ControlCAN*/ControlCANx64' % (pkg))[0].rstr()
        self.CPPPATH += [ControlCAN]
        if not IsBuildForWin32():  # mingw32 has no dlfcn
            self.LIBS += ['dl']
            self.CPPDEFINES += ['USE_ZLG_DLL']
        else:
            self.LIBS += ['ControlCAN']
            self.LIBPATH += [BUILD_DIR]
            shutil.copy('%s/ControlCAN.lib' %
                        (ControlCAN), '%s/libControlCAN.a' % (BUILD_DIR))

    def config(self):
        self.include = '%s/include' % (CWD)
        self.CPPPATH = ['$INFRAS']
        self.CPPDEFINES = []
        self.LIBPATH = []
        self.LIBS = ['TcpIp', 'StdTimer', 'Utils']
        self.CPPFLAGS = []
        self.source = objs
        if IsBuildForWindows():
            self.LIBS += ['wsock32', 'Ole32']
            self.config_peakcan()
            self.config_vxl()
            self.config_zlg()


objsCanSim = Glob('utils/can_simulator.c')


@register_application
class ApplicationCanSimulator(Application):
    def config(self):
        self.CPPPATH = ['$INFRAS']
        if IsBuildForWindows():
            self.LIBS = ['TcpIp', 'StdTimer', 'wsock32', 'winmm']
        self.source = objsCanSim


objsCanDump = Glob('utils/can_dump.c')


@register_application
class ApplicationCanDump(Application):
    def config(self):
        self.LIBS = ['CanLib']
        self.CPPPATH = ['$INFRAS']
        self.source = objsCanDump


objsCanSend = Glob('utils/can_send.c')


@register_application
class ApplicationCanSend(Application):
    def config(self):
        self.LIBS = ['CanLib']
        self.CPPPATH = ['$INFRAS']
        self.source = objsCanSend


objsCanBr = Glob('utils/can_bridge.c')


@register_application
class ApplicationCanBridge(Application):
    def config(self):
        self.LIBS = ['CanLib']
        self.CPPPATH = ['$INFRAS']
        self.source = objsCanBr